package org.ruoyi.interview.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.ruoyi.common.core.utils.MapstructUtils;
import org.ruoyi.common.core.utils.StringUtils;
import org.ruoyi.common.mybatis.core.page.PageQuery;
import org.ruoyi.common.mybatis.core.page.TableDataInfo;
import org.ruoyi.interview.domain.InterviewQuestionTag;
import org.ruoyi.interview.domain.bo.InterviewQuestionTagBo;
import org.ruoyi.interview.domain.vo.InterviewQuestionTagVo;
import org.ruoyi.interview.mapper.InterviewQuestionTagMapper;
import org.ruoyi.interview.service.IInterviewQuestionTagService;

import java.util.Collection;
import java.util.List;

/**
 * 题目标签Service业务层实现
 *
 * @author ruoyi
 */
@RequiredArgsConstructor
@Service
public class InterviewQuestionTagServiceImpl implements IInterviewQuestionTagService {

    private final InterviewQuestionTagMapper baseMapper;

    /**
     * 查询题目标签
     *
     * @param tagId 题目标签主键
     * @return 题目标签
     */
    @Override
    public InterviewQuestionTagVo selectInterviewQuestionTagById(Long tagId) {
        return baseMapper.selectVoById(tagId);
    }

    /**
     * 查询题目标签列表
     *
     * @param bo 题目标签业务对象
     * @param pageQuery 分页对象
     * @return 题目标签集合
     */
    @Override
    public TableDataInfo<InterviewQuestionTagVo> selectInterviewQuestionTagList(InterviewQuestionTagBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<InterviewQuestionTag> lqw = buildQueryWrapper(bo);
        Page<InterviewQuestionTagVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询题目标签列表
     *
     * @param bo 题目标签业务对象
     * @return 题目标签集合
     */
    @Override
    public List<InterviewQuestionTagVo> selectInterviewQuestionTagList(InterviewQuestionTagBo bo) {
        LambdaQueryWrapper<InterviewQuestionTag> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 新增题目标签
     *
     * @param bo 题目标签业务对象
     * @return 结果
     */
    @Override
    public Boolean insertInterviewQuestionTag(InterviewQuestionTagBo bo) {
        InterviewQuestionTag add = MapstructUtils.convert(bo, InterviewQuestionTag.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTagId(add.getTagId());
        }
        return flag;
    }

    /**
     * 修改题目标签
     *
     * @param bo 题目标签业务对象
     * @return 结果
     */
    @Override
    public Boolean updateInterviewQuestionTag(InterviewQuestionTagBo bo) {
        InterviewQuestionTag update = MapstructUtils.convert(bo, InterviewQuestionTag.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 批量删除题目标签
     *
     * @param tagIds 需要删除的题目标签主键集合
     * @return 结果
     */
    @Override
    public Boolean deleteInterviewQuestionTagByIds(Collection<Long> tagIds) {
        return baseMapper.deleteBatchIds(tagIds) > 0;
    }

    /**
     * 构建查询对象
     *
     * @param bo 题目标签业务对象
     * @return 查询对象
     */
    private LambdaQueryWrapper<InterviewQuestionTag> buildQueryWrapper(InterviewQuestionTagBo bo) {
        LambdaQueryWrapper<InterviewQuestionTag> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTagName()), InterviewQuestionTag::getTagName, bo.getTagName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), InterviewQuestionTag::getStatus, bo.getStatus());
        lqw.orderByAsc(InterviewQuestionTag::getTagId);
        return lqw;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(InterviewQuestionTag entity) {
        // TODO 根据业务逻辑添加校验
    }
}